package com.ruoyi.cmemetal.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.cmecomon.domain.ZxOrderPostman;
import com.ruoyi.cmecomon.mapper.ZxOrderPostmanMapper;
import com.ruoyi.cmecomon.service.IZxOrderPostmanService;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.ISysDictDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.cmemetal.mapper.ZxDataPreprocessMapper;
import com.ruoyi.cmemetal.domain.ZxDataPreprocess;
import com.ruoyi.cmemetal.service.IZxDataPreprocessService;
import org.springframework.transaction.annotation.Transactional;

/**
 * DataPreprocessService业务层处理
 *
 * @author zhangxu
 * @date 2021-12-28
 */
@Service
public class ZxDataPreprocessServiceImpl implements IZxDataPreprocessService
{
    @Autowired
    private ZxDataPreprocessMapper zxDataPreprocessMapper;

    @Autowired
    private ZxOrderPostmanMapper zxOrderPostmanMapper;

    @Autowired
    private ISysDictDataService sysDictDataService;

    @Autowired
    private IZxOrderPostmanService zxOrderPostmanService;

    /**
     * 查询DataPreprocess
     *
     * @param id DataPreprocess主键
     * @return DataPreprocess
     */
    @Override
    public ZxDataPreprocess selectZxDataPreprocessById(Long id)
    {
        return zxDataPreprocessMapper.selectZxDataPreprocessById(id);
    }

    /**
     * 查询DataPreprocess列表
     *
     * @param zxDataPreprocess DataPreprocess
     * @return DataPreprocess
     */
    @Override
    public List<ZxDataPreprocess> selectZxDataPreprocessList(ZxDataPreprocess zxDataPreprocess)
    {
        List<ZxDataPreprocess> zxDataPreprocesses = zxDataPreprocessMapper.selectZxDataPreprocessList(zxDataPreprocess);
        String dictType = "sys_cme_beforeman";
        for (ZxDataPreprocess dataPreprocess : zxDataPreprocesses) {

            List<Integer> integers = zxOrderPostmanService.selectOrderPostmenDictByOrderId(dataPreprocess.getOrderId());
            if(StringUtils.isNotNull(integers)){
                String dataPreprocessor = null;
                for (Integer integer : integers) {
                    String s = sysDictDataService.selectDictLabel(dictType, integer + "");
                    if(dataPreprocessor == null){
                        dataPreprocessor = s;
                    }else {
                        dataPreprocessor = dataPreprocessor + "," + s;
                    }
                }
                dataPreprocess.setDataPreprocessor(dataPreprocessor);
            }
        }

        return zxDataPreprocesses;
    }

    /**
     * 新增DataPreprocess
     *
     * @param zxDataPreprocess DataPreprocess
     * @return 结果
     */
    @Override
    public int insertZxDataPreprocess(ZxDataPreprocess zxDataPreprocess)
    {
        return zxDataPreprocessMapper.insertZxDataPreprocess(zxDataPreprocess);
    }


    /**
     * 新增订单前处理人员
     *
     * @param zxDataPreprocess
     */
    @Override
    public ZxDataPreprocess insertOrderPostmen(ZxDataPreprocess zxDataPreprocess) {

        Long[] postmen = zxDataPreprocess.getOrderPostmen();

        List<ZxOrderPostman> zxOrderPostmen = new ArrayList<>();

        String dataPreprocessor = null;
        String dictType = "sys_cme_beforeman";
        ZxDataPreprocess zxDataPreprocess1 = null;

        if(StringUtils.isNotNull(postmen)){
            for (Long postman : postmen) {
                if(dataPreprocessor == null){
                    dataPreprocessor = sysDictDataService.selectDictLabel(dictType,postman + "");
                }else {
                    dataPreprocessor = dataPreprocessor + "," + sysDictDataService.selectDictLabel(dictType,postman + "");
                }
                ZxOrderPostman zxOrderPostman = new ZxOrderPostman();
                zxOrderPostman.setOrderId(zxDataPreprocess.getOrderId());
                zxOrderPostman.setDictValue(postman.intValue());
                zxOrderPostmen.add(zxOrderPostman);
            }
            zxDataPreprocess.setDataPreprocessor(dataPreprocessor);

            zxDataPreprocess1 = zxDataPreprocess;

            zxOrderPostmanMapper.batchOrderPostman(zxOrderPostmen);
        }

        return zxDataPreprocess1;
    }

    /**
     * 修改DataPreprocess
     *
     * @param zxDataPreprocess DataPreprocess
     * @return 结果
     */
    @Override
    @Transactional
    public int updateZxDataPreprocess(ZxDataPreprocess zxDataPreprocess)
    {
        //删除原来订单的前处理人员
        zxOrderPostmanMapper.deleteZxOrderPostmanByOrderId(zxDataPreprocess.getOrderId());
        //新增订单前处理人员
        ZxDataPreprocess zxDataPreprocess1 = insertOrderPostmen(zxDataPreprocess);

        int i;
        if(zxDataPreprocess1 == null){
            i =  zxDataPreprocessMapper.updateZxDataPreprocess(zxDataPreprocess);
        }else {
            i =  zxDataPreprocessMapper.updateZxDataPreprocess(zxDataPreprocess1);
        }
        return i;
    }






    /**
     * 批量删除DataPreprocess
     *
     * @param ids 需要删除的DataPreprocess主键
     * @return 结果
     */
    @Override
    public int deleteZxDataPreprocessByIds(Long[] ids)
    {
        return zxDataPreprocessMapper.deleteZxDataPreprocessByIds(ids);
    }

    /**
     * 删除DataPreprocess信息
     *
     * @param id DataPreprocess主键
     * @return 结果
     */
    @Override
    public int deleteZxDataPreprocessById(Long id)
    {
        return zxDataPreprocessMapper.deleteZxDataPreprocessById(id);
    }
}
